<!DOCTYPE html>
<html>
<head>
    <title>学生管理</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.1.3/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-4">
    <h2>学生管理</h2>
    
    <!-- 添加学生表单 -->
    <form method="post" class="mb-4">
        {% csrf_token %}
        <div class="row g-3">
            <div class="col-md-5">
                <input type="text" name="name" placeholder="学生姓名" required class="form-control">
            </div>
            <div class="col-md-5">
                <select name="class_name" class="form-select" required>
                    <option value="">选择班级</option>
                    {% for class in classes %}
                    <option value="{{ class.id }}">{{ class.name }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="col-md-2">
                <button type="submit" name="add_student" class="btn btn-primary w-100">添加学生</button>
            </div>
        </div>
    </form>

    <!-- 学生列表 -->
    <table class="table table-striped">
        <thead>
            <tr>
                <th>学生姓名</th>
                <th>所属班级</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% for student in students %}
            <tr>
                <td>
                    <form method="post" class="d-inline">
                        {% csrf_token %}
                        <input type="hidden" name="student_id" value="{{ student.id }}">
                        <input type="text" name="name" value="{{ student.name }}" class="form-control form-control-sm">
                </td>
                <td>
                    <select name="class_name" class="form-select form-select-sm">
                        {% for class in classes %}
                        <option value="{{ class.id }}" {% if class.id == student.class_name.id %}selected{% endif %}>
                            {{ class.name }}
                        </option>
                        {% endfor %}
                    </select>
                </td>
                <td>
                    <div class="btn-group">
                        <button type="submit" name="edit_student" class="btn btn-sm btn-warning">保存</button>
                    </form>
                    <form method="post" class="d-inline">
                        {% csrf_token %}
                        <input type="hidden" name="delete_student" value="{{ student.id }}">
                        <button type="submit" class="btn btn-sm btn-danger">删除</button>
                    </form>
                    </div>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
</body>
</html>